<template>
  <div class="detail-box">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="300px">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">确定</el-button>
              <el-button @click="$router.back()">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second"><info /></el-tab-pane>
        <el-tab-pane label="岗位信息" name="third"><job /> </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 接口
import { getMoreInfo } from '../../api/user'
import { saveUserDetailById } from '../../api/employees'
// 组件
import info from './components/info'
import job from './components/job.vue'
export default {
  name: 'Detail',
  components: { info, job },
  data() {
    return {
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '长度在 6到 9 个字符', trigger: 'blur' }
        ]
      },
      userInfo: {}
    }
  },
  async created() {
    const res = await getMoreInfo({ id: this.$route.params.id })
    console.log('res', res)
    // 保存用户数据
    this.userInfo = res
    this.form.username = res.username
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const res1 = await saveUserDetailById({
            ...this.userInfo,
            ...this.form
          })
          this.$message.success('修改成功')
          this.$router.push({ path: '/employees' })
          console.log('res1', res1)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.detail-box {
  padding: 20px;
  .el-input__inner {
    width: 300px;
  }
}
</style>
